import Vue from "vue";

import Vuex from 'vuex';
Vue.use(Vuex);
import axios from "axios";

var store = new Vuex.Store({
    state:{
        list:[],
        four:[],
        five:[],
        leibie:[],
        choice:[],
        news:[],
        groom:[],
        userinfo:'',
        bkind:[],
        bigKindItem:{},
        itemskindAll:[],
        skindItem:{},
        showCar:false,
        findchart:[],
        cartItemReg:[],
        goodsAll:[],
        listItem:"ItemH",
        // 升序true，降序false(排序)
        sortby: false,
    },
    mutations:{
         
        setList:function(state,value){
            state.list = value;
        },
        setFour:function(state,value){
            state.four = value;
        },
        setFive:function(state,value){
            state.five = value;
        },
        setLeibie:function(state,value){
            state.leibie = value;
        },
        setChoice:function(state,value){
            state.choice = value;
        },
        setNews:function(state,value){
            state.news = value;
        },
        setGroom:function(state,value){
            state.groom  = value;
        },
        setUserinfo:function(state,value){
            state.userinfo  = value;
        },
        setBkind:function(state,value){
            state.bkind  = value;
        },
         setbigKindItem:function(state,value){
            state.bigKindItem = value;
        },
        setItemskindAll:function(state,value){
            state.itemskindAll = value;
        },
         setSkindItem:function(state,value){
            state.skindItem = value;
        },
        setShowCar:function(state,value){
            state.showCar = value;
        },
        // 用户购物车商品
        setFindchart:function(state,value){
            state.findchart = value;
        },
        // 用户购物车编辑某件商品传规格
        setCartItemReg:function(state,value){
            state.cartItemReg = value;
        },
        setGoodsAll:function(state,value){
            state.goodsAll = value;
        },
        // 改变商品列表布局
        changeItem:function(state) {
            if (state.listItem == "ItemH") {
                state.listItem = "ItemV";
            } else {
                state.listItem = "ItemH";
            }
        },
        // 设置升降序
        setBy:function(state) {
            state.sortby = !state.sortby;
        },
    },
    actions:{
      
        // 请求首页轮播图
        requestList:function(state,data){
            axios.get("/api/lunbo")
            .then(res=>{
                state.commit("setList",res.data.list);
                state.commit("setFour",res.data.four);
                state.commit("setFive",res.data.five);
                state.commit("setLeibie",res.data.leibie);
                state.commit("setChoice",res.data.choice);
                state.commit("setNews",res.data.news);
                state.commit("setGroom",res.data.groom)
            })
        },
        // // 提交注册
        // requestRegist:function(context,data){
        //     return new Promise(function(resolve,reject){
        //         axios.post("/api/users/regist",data).then(res=>{
        //             resolve(res);
        //         });
        //     });
        // },
        // 提交登录
        requestLogin:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/login",data).then(res=>{
                    resolve(res);
                });
            });
        },
        // 请求是否登录
        requestIslogin:function(context,data){
            return new Promise(function(resolve,reject){
                axios.post("/api/users/islogin")
                .then(res=>{
                    resolve(res);
                });
            });
        },
        // 请求大分类
        requestBkind:function(state,data){
            axios.post("/api/bigkind")
            .then(res=>{
                state.commit("setBkind",res.data.data)
                 var param = {
                    bigkind:res.data.data[0].bigkind
                }
                this.dispatch("requestBigKindItem",param);
                console.log(res.data)
            })
        },
        //请求一个大分类的所有小分类
        requestBigKindItem:function(context,pramas){
            axios.post("/api/bigkinditem",pramas)
            .then(res=>{   
                res.data.data.img = "/imgs/bigkind/"+res.data.data.img;
                context.commit("setbigKindItem",res.data.data);
            });
        },
         // 请求某个小类的所有商品
        requestItemskindAll:function(context,params){
            axios.get("/api/itemskindAll",{params})
            .then(res=>{ 
                context.commit("setItemskindAll",res.data.data);
            })
        },
        requestSkindItem:function(context,params){
                axios.post("/api/skindItem",params)
                .then(res=>{
                    context.commit("setSkindItem",res.data.data)
                });
        },
        //添加商品规格
        requestAddregGoods:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/addregGoods",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户加入购物车请求
        requestChartAdd:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/chartadd",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户购物车数据
        requestFindchart:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/findchart",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户购物车修改商品数量和商品规格
        requestEditChart:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/editchart",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户删除购物车商品
        requestDeleteChart:function(context,params){
            return new Promise(function(resolve,reject){
                axios.post("/api/deletechart",params)
                .then(res=>{
                    resolve(res);
                });
            })
        },
        // 用户删除购物车商品
        requestGoodsAll:function(context,params){
            axios.get("/api/goodsAll",params)
            .then(res=>{
                context.commit("setGoodsAll",res.data.data)
                console.log(res.data.data)
            });
           
        },
    }
})


export default store;